﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO;

namespace CatDb
{
    public partial class Form1 : Form
    {

        CatDb _catDb = new CatDb();
        List<Cat> _cats = new List<Cat>();


        public Form1() {
            InitializeComponent();
            string dbFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CatDb");
            _catDb.open(dbFile);
            _cats.AddRange(_catDb.getAllCats());
        }

        private void readAllCatsFromDatabase() {
            _cats.Clear();
            _cats.AddRange(_catDb.getAllCats());
        }

        private class CatListViewItem : ListViewItem {
            
            private Cat _cat;
            
            public Cat Cat { get { return _cat; } }

            public CatListViewItem(Cat cat) {
                _cat = cat;
                this.Text = cat.Id.ToString();
                SubItems.Add(cat.Name);
                SubItems.Add(cat.Gender.ToString());
                SubItems.Add(cat.Age.ToString());
                SubItems.Add(cat.Description);
                SubItems.Add(cat.Neutered ? "Neutered" : "Not Neutered");
            }

        }

        private void fillListView() {
            _listView.Items.Clear();
            foreach (Cat cat in _cats) {
                CatListViewItem item = new CatListViewItem(cat);
                _listView.Items.Add(item);
            }
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            
            fillListView();

        }

        private void _bn_edit_Click(object sender, EventArgs e) {
            if (_listView.SelectedItems.Count > 0) {
                CatListViewItem item = (CatListViewItem)_listView.SelectedItems[0];
                CatDetailsForm form = new CatDetailsForm(item.Cat);
                if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                    _catDb.saveCat(item.Cat);
                    readAllCatsFromDatabase();
                    fillListView();
                }
            }
        }

        private void _bn_new_Click(object sender, EventArgs e) {
            Cat cat = new Cat(-1, 0, Gender.female, false, "<unnamed>", "");
            CatDetailsForm form = new CatDetailsForm(cat);
            if (form.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                _catDb.saveCat(cat);
                readAllCatsFromDatabase();
                fillListView();
            }

        }

        private void _bn_remove_Click(object sender, EventArgs e) {
            if (_listView.SelectedItems.Count > 0) {
                CatListViewItem item = (CatListViewItem)_listView.SelectedItems[0];
                Cat cat = item.Cat;
                _catDb.deleteCat(cat);
                readAllCatsFromDatabase();
                fillListView();
            }

        }


    }
}
